﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public partial class UserControl_OpenFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["type"] != null)
        {
            string type = Request.QueryString["type"].ToString();
            switch (type)
            {
                case "0":
                case "1":
                    DownloadFile(Request.QueryString["name"], Request.QueryString["id"], type);
                    break;
                case "-1":
                    DelTmpNewFile(Request.QueryString["newName"].ToString());//移除文件时删除临时文件夹里的文件
                    break;
                case "-2":
                    AddTmpNewFile(Request.QueryString["newName"].ToString());
                    break;
                default:
                    break;
            }
        }

    }
    private void DelTmpNewFile(string newName)
    {
        string path = Server.MapPath("../TmpFile/") + newName;
        if (System.IO.File.Exists(path))
            System.IO.File.Delete(path);
        //else
        //{
        //    path = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir") + "/" + newName;
        //    if (System.IO.File.Exists(path))
        //        System.IO.File.Delete(path);
        //}
    }
    private void AddTmpNewFile(string newName)
    {
        string path = Server.MapPath("../TmpFile/") + newName;
        string _folder = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir");
        if (!System.IO.File.Exists(path))
            System.IO.File.Copy(_folder + "/" + newName, Server.MapPath("../TmpFile/") + newName, true);

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Write(_folder);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
    private void DownloadFile(string filename, string fileid, string filetype)
    {
        string filePath = "";
        if (filetype == "0")
            filePath = Server.MapPath("../TmpFile/") + fileid;
        if (filetype == "1")
            filePath = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir") + "/" + fileid;
        if (!File.Exists(filePath))
        {
            // 文件没有找到 
            Page.Response.Write("文件不存在");

            return;
        }

        // 真实的文件名 
        // string fileName = System.IO.Path.GetFileName(filePath);
        string fileName = filename;
        if (fileName == string.Empty)
        {
            //文件没有找到 
            Page.Response.Write("文件不存在");
            return;
        }


        string f = fileName;
        Response.Clear();
        Page.Response.ContentType = "APPLICATION/OCTET-STREAM";

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Page.Response.WriteFile(filePath);
        Response.Flush();
    }
}
